package database;

import toDo.app.models.User;
import android.content.ContentValues;
import android.content.Context;

/**
 * this is the users table, make more tables in a similar fashion
 * however, for M6 we don't need more tables
 * @author Sparky
 *
 */
class UserDatabaseConnector extends DatabaseTableTemplate {
	public static final String TABLE_NAME = "users"; //$NON-NLS-1$
	public static final String COLUMN_ID = "_id"; //$NON-NLS-1$
	public static final String COLUMN_USERNAME = "username"; //$NON-NLS-1$
	public static final String COLUMN_PASSWORD = "password"; //$NON-NLS-1$
	
	public static final String DATABASE= "CREATE TABLE " + TABLE_NAME + "( "  //$NON-NLS-1$ //$NON-NLS-2$
			+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "  //$NON-NLS-1$
			+ COLUMN_USERNAME + " TEXT UNIQUE NOT NULL, "  //$NON-NLS-1$
			+ COLUMN_PASSWORD + " TEXT NOT NULL);"; //$NON-NLS-1$
	
	public UserDatabaseConnector(Context context) {
		super(context);
	}
	
	public User addUser(User user){
		if(hasUser(user)){
			return user;
		}
		doOpen();
		ContentValues values = new ContentValues();
		values.put(COLUMN_USERNAME, user.getUsername());
		values.put(COLUMN_PASSWORD, user.getPassword());
		this.connector.insert(TABLE_NAME, null, values);
		doClose();
		int id = getUserId(user);
		return new User(user.getUsername(),user.getPassword(),id);
	}
	
	/**
	 * checks for user
	 * @param user user to check
	 * @return whether user present
	 */
	public boolean hasUser(User user){
		doOpen();
		boolean value = false;
	//	try{
			this.cursor = this.connector.query(TABLE_NAME,
					new String[]{COLUMN_USERNAME}, 
					COLUMN_USERNAME + " = ?",  //$NON-NLS-1$
					new String[]{user.getUsername()}, null, null, null);
			this.cursor.moveToFirst();
			if(this.cursor.getCount() == 1){
				if(this.cursor.getString(0).equals(user.getUsername())){
					value = true;
				}
				
			}
		//}catch(FatalException e){
			
		//}
		doClose();
		return value;
	}
	
	public User isValidUser(User user) {
		doOpen();
		User out = user;
		this.cursor = this.connector.query(TABLE_NAME,
				new String[]{COLUMN_ID,COLUMN_USERNAME,COLUMN_PASSWORD}, COLUMN_USERNAME + " = '" + user.getUsername()+"'", null, //$NON-NLS-1$ //$NON-NLS-2$
				null, null, null);
		this.cursor.moveToFirst();
		if(this.cursor.getCount() == 1){
			if(this.cursor.getString(1).equals(user.getUsername()) && this.cursor.getString(2).equals(user.getPassword())){
				int id = this.cursor.getInt(0);
				out =  new User(user.getUsername(),user.getPassword(),id);
			}
			
		}
		doClose();
		return out;
	}
	
	public int getUserId(User user){
			doOpen();
			int id = -1;
			this.cursor = this.connector.query(TABLE_NAME,
					new String[]{COLUMN_ID,COLUMN_USERNAME,COLUMN_PASSWORD}, COLUMN_USERNAME + " = '" + user.getUsername()+"'", null, //$NON-NLS-1$ //$NON-NLS-2$
					null, null, null);
			this.cursor.moveToFirst();
			if(this.cursor.getCount() == 1){
				if(this.cursor.getString(1).equals(user.getUsername()) && this.cursor.getString(2).equals(user.getPassword())){
					id = this.cursor.getInt(0);
				}
				
			}
			doClose();
			return id;
	}
	
	@Override
	protected String getTableStructure() {
		return DATABASE;
	}
}
